package com.gdcp.service.impl;

import com.gdcp.beans.Category;
import com.gdcp.beans.OrderItem;
import com.gdcp.beans.Product;
import com.gdcp.common.ResponseModel;
import com.gdcp.mapper.OrderItemMapper;
import com.gdcp.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    ResponseModel responseModel;

    @Override
    public Map<String, Object> findPageBreakByCondition(Map<String, Object> model, Integer shop_id) {
        //bootstrap-table要求服务器返回的json须包含：totlal，rows
        Map<String,Object> result = new HashMap<>();
        int total=orderItemMapper.findPageBreakByConditionByShopId(shop_id).size();
        List<OrderItem> rows=orderItemMapper.findPageBreakByCondition(model);
        /*for (OrderItem orderItem:rows){
            System.out.println(orderItem);
        }*/
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>=0) {
                model.put("a", count);
                rows = orderItemMapper.findPageBreakByCondition(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public Map<String, Object> removeByPrimaryKey(Integer[] ids) {
        try{
            for (Integer id:ids) {
                orderItemMapper.removeByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return responseModel.setModel(false,"删除["+ids.length+"条记录失败"+e.toString());
        }

        return responseModel.setModel(true,"成功删除["+ids.length+"条记录");
    }

    @Override
    public int updateInformation(OrderItem orderItem) {
        try{
            orderItemMapper.updateByPrimaryKey(orderItem);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    @Override
    public OrderItem getInformation(Integer id) {
        return orderItemMapper.getInformation(id);
    }

    @Override
    public Map<String, Object> searchInformation(Map<String,Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=orderItemMapper.searchInformation(null).size();
        List<OrderItem> rows=orderItemMapper.searchInformation(model);
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public Map<String, Object> findAll(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=orderItemMapper.findAll(null).size();
        List<OrderItem> rows=orderItemMapper.findAll(model);
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>=0) {
                model.put("a", count);
                rows = orderItemMapper.findAll(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public Map<String, Object> searchInformationByShopId(Map<String, Object> model, Integer shop_id) {
        Map<String,Object> result = new HashMap<>();
        int total=orderItemMapper.searchSizeByShopId(shop_id).size();
        List<Category> rows=orderItemMapper.searchInformationByShopId(model);
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    public int insert(OrderItem orderItem) {
        try {
            orderItemMapper.insert(orderItem);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public List<OrderItem> queryOrderItemByOid(String id) {
        return orderItemMapper.queryOrderItemByOid(id);
    }
}
